home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / brwsr.2 ƒ / file_utils.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-13  |  2.4 KB  |  111 lines  |  [TEXT/KAHL]

  1. /* initialize, open, etc. various things for brwsr program...
  2.  * 870805-13... ^z
  3.  */
  4.  
  5. #include <stdio.h>            /* for FILE, getc(), etc. */
  6. #include <strings.h>        /* for strcpy(), strcat(), etc. */
  7. #include <unix.h>            /* for exit(), time(), etc. */
  8. #include <proto.h>            /* for function prototypes */
  9. #include "brwsr.h"            /* for various definitions */
  10. #include "brwsr.proto.h"    /* for my function prototypes */
  11.  
  12.  
  13. /* open the document, key, and pointer files ...
  14.  * for now, demand that if the document file is named * then the
  15.  * key file must be named *.k and the pointer file must be *.p
  16.  * ... just the way that the ndxr program builds the index.....
  17.  */
  18.  
  19. void open_files (cmd)
  20.   char cmd[];
  21.   {
  22.     char ocmd[STRLEN], *strcat(), *strcpy();
  23.       FILE *fopen();
  24.       extern FILE *doc_file, *key_file, *ptr_file;
  25.  
  26.     close_files ();
  27.     strcpy (ocmd, cmd + 1);
  28.     if ((doc_file = fopen (ocmd, "r")) == NULL)
  29.       {
  30.         beep ();
  31.         printf ("Error opening document file \"%s\"!\n", ocmd);
  32.         close_files ();
  33.         return;
  34.       }
  35.  
  36.     strcat (ocmd, ".k");
  37.     if ((key_file = fopen (ocmd, "rb")) == NULL)
  38.       {
  39.         beep ();
  40.         printf ("Error opening key file \"%s\"!\n", ocmd);
  41.         close_files ();
  42.         return;
  43.       }
  44.  
  45.     ocmd[strlen (ocmd) - 2] = '\0';
  46.     strcat (ocmd, ".p");
  47.     if ((ptr_file = fopen (ocmd, "rb")) == NULL)
  48.       {
  49.         beep ();
  50.         printf ("Error opening ptr file \"%s\"!\n", ocmd);
  51.         close_files ();
  52.         return;
  53.       }
  54.   }
  55.  
  56.  
  57. /* close off the document, key, and pointer files
  58.  */
  59.  
  60. void close_files ()
  61.   {
  62.     extern FILE *doc_file, *key_file, *ptr_file;
  63.     
  64.     if (doc_file != NULL)
  65.       {
  66.         fclose (doc_file);
  67.         doc_file = NULL;
  68.       }
  69.     if (key_file != NULL)
  70.       {
  71.         fclose (key_file);
  72.         key_file = NULL;
  73.       }
  74.     if (ptr_file != NULL)
  75.       {
  76.         fclose (ptr_file);
  77.         ptr_file = NULL;
  78.       }
  79.   }
  80.  
  81.  
  82. /* set up initial values for current_item and max_item arrays ...
  83.  * initially, current_item is set to an illegal value, -1, and
  84.  * max_item[CONTEXT] and max_item[TEXT] convey information about
  85.  * the sizes of the pointer and document files respectively...
  86.  */
  87.  
  88. void init_items ()
  89.   {
  90.     extern long current_item[], max_item[];
  91.     extern FILE *doc_file, *key_file, *ptr_file;
  92.     extern int level;
  93.     
  94.     level = INDEX;
  95.     
  96.     current_item[INDEX] = -1;
  97.     current_item[CONTEXT] = -1;
  98.     current_item[TEXT] = -1;
  99.  
  100.     fseek (key_file, 0L, 2);
  101.     max_item[INDEX] = ftell (key_file) / sizeof (KEY_REC) - 1;
  102.     
  103.     fseek (ptr_file, 0L, 2);
  104.     max_item[CONTEXT] = ftell (ptr_file) / sizeof (PTR_REC) - 1;
  105.  
  106.     fseek (doc_file, 0L, 2);
  107.     max_item[TEXT] = ftell (doc_file) - 1;
  108.   }
  109.  
  110.  
  111.